/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-07-01 15:47:43
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-11-16 17:37:39
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MaterialsServiceProxy, MaterialsNodeEditDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
@Component({
    selector: 'treeAddOrEdit',
    templateUrl: './tree-add-or-edit.component.html',
    styleUrls: ['./tree-add-or-edit.component.less']
})
export class TreeAddOrEditComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Input() parentrow: any;//当前点击节点
    // @Input() bopInfo:any;//当前bopInfo
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    saving = false;
    active = false;
    form = {
        materialTypeName: ''
    };//新增树结构实体
    isInsert: true;//是否是新增 true为新增
    saveFlag = true;
    constructor(
        injector: Injector,
        private _materialsServiceProxy: MaterialsServiceProxy,
        private _messageService: MessageService,
    ) {
        super(injector);
    }

    ngOnInit() {
    }
    //保存树结构
    save(): void {
        //获取实体
        let input = new MaterialsNodeEditDto();
        if (this.isInsert) {
            input.id = "00000000-0000-0000-0000-000000000000";
        } else {
            input.id = this.parentrow.id;
        }
        input.parentCode = this.parentrow.id;
        input.materialTypeName = this.form.materialTypeName;
        input.levelNum = this.parentrow.levelNum;
        input.materialsDescId = 'fa568b8c-4eb5-47b1-92b1-f5aaff234de4';
        if (this.parentrow.materialTypeCode) {
            input.materialTypeCode = this.parentrow.materialTypeCode;
        }
        this.saving = true;
        if (this.saveFlag) {
            this.saveFlag = false;
            this._materialsServiceProxy.createOrUpdateMaterialsNode(input)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe((result) => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('保存成功');
                        this.close();
                        this.modalSave.emit(true);
                    } else {
                        this._messageService.error(result.message)
                    }

                });
        }

    }
    //打开弹窗
    onShown(): void {

    }
    //查看新增任务
    show(key, row): void {
        this.isInsert = key;
        if (!key) {
            this.form.materialTypeName = row.materialTypeName;
        }
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.saveFlag = true;
        this.form.materialTypeName = '';
        this.active = false;
        this.modal.hide();
    }

}
